//这个文件用于检查notification

shanbay.ntf = (function() {
  var that = this;
  
  //白名单，在这些页面上不刷新通知
  var whitelist = ["http://www.shanbay.com/review/",
                   "http://www.shanbay.com/read/",];

  var refresh_interval = 5 * 60 * 1000;
  
  var id_ntf_msg = 13;
  var id_ntf_reply = 19;
  var url_ntf_msg = "http://www.shanbay.com/notification/" + id_ntf_msg;
  var url_ntf_reply = "http://www.shanbay.com/notification/" + id_ntf_reply;
  var id_chattingspan = {};
  id_chattingspan[id_ntf_reply] = "reply";
  id_chattingspan[id_ntf_msg] = "msg";

  var id_ntf = {};
  
  var get_ntf_url_and_clear = function(id, url) {
    if (!(id in id_ntf)) return;
    clear_ntf(id);
    return url;
  };
  
  this.get_ntf_msg_url = function() {
    return get_ntf_url_and_clear(id_ntf_msg, url_ntf_msg);
  };
  
  this.get_ntf_reply_url = function() {
    return get_ntf_url_and_clear(id_ntf_reply, url_ntf_reply);
  };
  
  var clear_ntf = function(id) {
    var $ntf = id_ntf[id];
    delete id_ntf[id];

    var $container = $ntf.parents("#notification-container:first");
    $ntf.remove();
    if ($.trim($container.text()) in {"x": null, "": null}) {
      $container.remove();
    }
  };

  var get_ntf_id_and_link_to_chatting = function($ntf) {
    var id = null;
    $ntf.find("a").each(function() {
      var match = this.href.match(/notification\/(\d+)\//);
      if (!match) return;

      id = parseInt(match[1]);
      if (id in id_chattingspan) {
        $(this).prop("href", "javascript:void(0)").click(function() {
          shanbay.chatting.show(id_chattingspan[id]);
        });
      }
      
      return false;
    });

    return id;
  };
  
  this.append_ntf = function($ntf) {
    var id = get_ntf_id_and_link_to_chatting($ntf);
    if (id in id_ntf) {
      id_ntf[id].replaceWith($ntf);
      id_ntf[id] = $ntf;
      return;
    }

    id_ntf[id] = $ntf;
    var $ntf_container = $("#left-container #notification-container");
    if ($ntf_container.size() == 0 || $.trim($ntf_container.text()) in {'':null, 'x':null}) {
      $ntf_container.remove();
      $ntf_container = $("<div id='notification-container'><div class='notifications'><ul class='notification'></ul></div></div>");
      $ntf_container.appendTo($("#left-container"));
    }

    $ntf_container.find("ul.notification").append($ntf);
  };
  
  this.handle_ntf = function() {
    $("#notification-container .notification li").each(function() {
      var id = get_ntf_id_and_link_to_chatting($(this));
      id_ntf[id] = $(this);
    });

    for (var i in whitelist) {
      if (window.location.href.indexOf(whitelist[i]) != -1) return;
    }

    window.setInterval(function() {
      $.get("http://www.shanbay.com/notification/get_notifications/", null, function(data) {
        if ("template_content" in data) {
          $(data.template_content).each(function() {
            that.append_ntf($(this));
          });
        }
      }, "json");
    }, refresh_interval);
  };

  return this;
})();

$(function() {
  shanbay.ntf.handle_ntf();
});
